package com.ciwong.epaper.modules.reciteWords.dbreciteword;


import android.text.TextUtils;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.db.DBCallBack;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public abstract class BaseDBManager {
    private WordDetailDao wordDetailDao;
    private String TAG = "BaseDBManager";
    private String NAME = "word_";
    public static String BOOK_ID = "";

    protected BaseDBManager() {
        wordDetailDao = new WordDetailDao();
    }

    /**
     * 获取数据库基本名称（最后在拼接bookid 或者versionid组成完整的表名）
     *
     * @return
     */
    private String getBaseTableName() {
        long userId = EApplication.getInstance().getUserInfoBase().getUserId();
        if (EApplication.BRAND_ID <= 0) {
            return null;
        }
        if (userId <= 0)
            return null;

        return NAME + EApplication.BRAND_ID + userId;
    }


    public boolean creatTable(String versionId) {
        String bookId = versionId;
        if (TextUtils.isEmpty(bookId)) bookId = BOOK_ID + "";
        boolean flag = true;
        String baseTableName = getBaseTableName();
        if (baseTableName == null)
            flag = false;
        if (wordDetailDao == null)
            wordDetailDao = new WordDetailDao();
        try {
            wordDetailDao.creatTable(baseTableName + bookId);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    public boolean dropTable(String versionId) {
        String bookId = versionId;
        if (TextUtils.isEmpty(bookId)) bookId = BOOK_ID + "";
        String baseTableName = getBaseTableName();
        if (baseTableName == null)
            return false;
        if (wordDetailDao == null)
            wordDetailDao = new WordDetailDao();
        wordDetailDao.dropTable(baseTableName + bookId);
        return true;
    }

    /**
     * 判断表是否存在
     *
     * @param versionId
     * @return
     */
    public boolean checkTableIsExist(String versionId) {
        String bookId = versionId;
        if (TextUtils.isEmpty(bookId)) bookId = BOOK_ID + "";
        String baseTableName = getBaseTableName();
        if (baseTableName == null)
            return false;
        if (wordDetailDao == null)
            wordDetailDao = new WordDetailDao();
        return wordDetailDao.checkTableIsExist(baseTableName + bookId);
    }

    /**
     * 判断表中是否有数据
     *
     * @param versionId
     * @return
     */
    public boolean checkTableHasData(String versionId) {
        String bookId = versionId;
        if (TextUtils.isEmpty(bookId)) bookId = BOOK_ID + "";
        String baseTableName = getBaseTableName();
        if (baseTableName == null)
            return false;
        if (wordDetailDao == null)
            wordDetailDao = new WordDetailDao();
        int rowCount = wordDetailDao.getRowCount(baseTableName + bookId);
        return rowCount <= 0 ? false : true;
    }

    protected boolean executeDeleteAll(String versionId) {
        String bookId = versionId;
        if (TextUtils.isEmpty(bookId)) bookId = BOOK_ID + "";
        String baseTableName = getBaseTableName();
        if (baseTableName == null)
            return false;
        if (wordDetailDao == null)
            wordDetailDao = new WordDetailDao();
        String tableName = baseTableName + bookId;
        if (!wordDetailDao.checkTableIsExist(tableName))
            wordDetailDao.creatTable(tableName);
        wordDetailDao.deleteAll(tableName);
        return true;
    }
}
